package com.ebb.develop;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

import com.ebb.R;
import com.ebb.classes.EBBActivity;
import com.ebb.classes.EBBAlert;
import com.ebb.classes.EBBUnreadCount;
import com.ebb.common.EBBConstants;
import com.parse.GetCallback;
import com.parse.ParseException;
import com.parse.ParseObject;
import com.parse.ParseQuery;
import com.parse.ParseUser;
import com.parse.SaveCallback;

public class EBBTestUnreadCount extends EBBActivity {
    private static final String TAG = EBBTestUnreadCount.class.getCanonicalName();
    private String testEchoObjectId = "ioAU9ZGoWF";
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.unread_count_test);
        login();
    }
    
    private void login() {
        try {
            ParseUser.logIn("sjune", "sjune");
            init();
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }
    
    private void init() {
        
        Button btnCreate = (Button) findViewById(R.id.unreadcount_test_button_c);
        Button btnGet = (Button) findViewById(R.id.unreadcount_test_button_g);
        Button btnUpdateUnreadCountToZero = (Button) findViewById(R.id.unreadcount_test_button_u);
        Button btnIncrement = (Button) findViewById(R.id.unreadcount_test_button_i);
        
        // create
        btnCreate.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View v) {
                Log.d(TAG, "create");
                
                final ParseObject echoObject = new ParseObject(EBBConstants.PARSE_CLASS_NAME_ECHO);
                echoObject.put("message", "test....");
                echoObject.put("userObject", ParseUser.getCurrentUser());
                echoObject.saveInBackground(new SaveCallback() {

                    @Override
                    public void done(ParseException e) {
                        if(e!=null) {
                            e.printStackTrace();
                            return;
                        }

                        testEchoObjectId = echoObject.getObjectId();
                        EBBUnreadCount.create(echoObject);
                    }
                });
   
            }
        });

        // increment
        btnIncrement.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View v) {
                Log.d(TAG, "update");
                
                ParseQuery parseQuery = new ParseQuery(EBBConstants.PARSE_CLASS_NAME_ECHO);
                parseQuery.getInBackground(testEchoObjectId, new GetCallback() {
                    
                    @Override
                    public void done(ParseObject echoObject, ParseException e) {
                        if(e!=null) {
                            e.printStackTrace();
                            return;
                        }
                        
                        Log.d(TAG, "echoObject >>> " + echoObject.getObjectId());
                        
                        EBBUnreadCount.increment(echoObject);
                    }
                });
                
            }
        });    
        

        // update
        btnUpdateUnreadCountToZero.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View v) {
                Log.d(TAG, "update");
                
                ParseQuery parseQuery = new ParseQuery(EBBConstants.PARSE_CLASS_NAME_ECHO);
                parseQuery.getInBackground(testEchoObjectId , new GetCallback() {
                    
                    @Override
                    public void done(ParseObject echoObject, ParseException e) {
                        if(e!=null) {
                            e.printStackTrace();
                            return;
                        }
                        
                        Log.d(TAG, "echoObject >>> " + echoObject.getObjectId());
                        
                        EBBUnreadCount.updateUnreadCountToZero(echoObject);
                    }
                });
                
            }
        });            
        
        // read
        btnGet.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View v) {
                Log.d(TAG, "read");
                
                ParseQuery parseQuery = new ParseQuery(EBBConstants.PARSE_CLASS_NAME_ECHO);
                
                try {
                    ParseObject echoObject = parseQuery.get(testEchoObjectId);
                    ParseObject unreadCountObject = EBBUnreadCount.getUnreadCountObject(echoObject);
                    EBBAlert.message(0, "unreadCount", "count : " + unreadCountObject.getInt("unreadCount"));
                } catch (ParseException e) {
                    e.printStackTrace();
                }
            }
        });
              
    }

}
